package city.spring.modules.reception.controller;

import city.spring.modules.reception.entity.OldmanActiveExtEntity;
import city.spring.modules.reception.repository.OldmanActiveExtRepository;
import city.spring.modules.reception.service.OldmanActiveExtService;
import city.spring.modules.reception.vo.OldmanActiveExtVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Controller：活动-老人联系表
 *
 * @author pxm
 * @date 2020-07-13 16:16:16
 */
@RestController
@RequestMapping("/oldman_active_ext/{activeId}/oldman")
public class OldmanActiveExtController extends ApiController {
    private final OldmanActiveExtService oldmanActiveExtService;
    private final OldmanActiveExtRepository oldmanActiveExtRepository;

    public OldmanActiveExtController(OldmanActiveExtService oldmanActiveExtService, OldmanActiveExtRepository oldmanActiveExtRepository) {
        this.oldmanActiveExtService = oldmanActiveExtService;
        this.oldmanActiveExtRepository = oldmanActiveExtRepository;
    }

    @ModelAttribute
    public void getActiveId(@PathVariable String activeId) {
        if (activeId == null) {
            throw new RuntimeException("活动记录ID必须不为空");
        }
    }

    /**
     * 获取活动记录的所有人员列表
     *
     * @param activeId 活动ID
     */
    @GetMapping
    public Object list(@PathVariable String activeId) {
        return success(oldmanActiveExtRepository.getActiveOldmans(activeId));
    }

    /**
     * 给活动记录添加多个人员信息
     *
     * @param activeId 活动ID
     * @param oldmans  所添加的人员信息列表
     * @return
     */
    @PostMapping
    public Object addAll(@PathVariable String activeId, @RequestBody List<OldmanActiveExtEntity> oldmans) {
        // 查出已有的数据
        List<OldmanActiveExtVO> dbOldmans = oldmanActiveExtRepository.getActiveOldmans(activeId);
        // 数据库已经存在的列表
        Set<String> dbOldmanIdsSet = dbOldmans.stream()
                .map(OldmanActiveExtEntity::getOldmanId)
                .collect(Collectors.toSet());
        // 过滤掉已经存在数据库的数据
        List<OldmanActiveExtEntity> needSaveOldmans = oldmans.stream()
                .filter(item -> {
                    item.setActiveId(activeId);
                    String oldmanId = item.getOldmanId();
                    // 过滤出不存在数据库中的数据
                    return oldmanId != null && !dbOldmanIdsSet.contains(oldmanId);
                }).collect(Collectors.toList());
        if (!needSaveOldmans.isEmpty()) {
            oldmanActiveExtService.saveBatch(needSaveOldmans);
        }

        return success(null);
    }

    /**
     * 获取活动的一个老人信息
     *
     * @param activeId 活动ID
     * @param extId    关联ID
     * @return
     */
    @GetMapping("{extId}")
    public Object getInfo(@PathVariable String activeId, @PathVariable String extId) {
        return success(oldmanActiveExtRepository.getActiveOldman(activeId, extId));
    }

    /**
     * 获取一个 <strong>活动-老人联系表</strong>
     *
     * @param id 主键ID
     */
//    @GetMapping("{id}")
//    public Object info(@PathVariable String id) {
//        return success(oldmanActiveExtService.getById(id));
//    }


    /**
     * 修改一个活动关联的老人信息
     *
     * @param activeId  活动ID
     * @param extId     关联ID
     * @param extEntity 接收的数据
     * @return
     */
    @PutMapping("{extId}")
    public Object update(@PathVariable String activeId, @PathVariable String extId, @RequestBody OldmanActiveExtEntity extEntity) {
        extEntity.setId(extId);
        extEntity.setActiveId(activeId);
        oldmanActiveExtService.updateOldmanActiveExt(extEntity);
        return success(null);
    }

    /**
     * 删除一个 <strong>活动记录的关联人员信息</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        oldmanActiveExtService.deleteOldmanActiveExt(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>活动-老人联系表</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        oldmanActiveExtService.deleteOldmanActiveExt(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<OldmanActiveExtEntity> buildLambdaQuery(OldmanActiveExtEntity entity) {
        LambdaQueryChainWrapper<OldmanActiveExtEntity> lambdaQuery = oldmanActiveExtService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), OldmanActiveExtEntity::getId, entity.getId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getActiveId()), OldmanActiveExtEntity::getActiveId, entity.getActiveId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getOldmanId()), OldmanActiveExtEntity::getOldmanId, entity.getOldmanId());
        return lambdaQuery;
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private QueryWrapper<OldmanActiveExtEntity> buildQuery(OldmanActiveExtEntity entity) {
        QueryWrapper<OldmanActiveExtEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(entity.getId()), "id", entity.getId());
        wrapper.like(StringUtils.isNotBlank(entity.getActiveId()), "active_id", entity.getActiveId());
        wrapper.like(StringUtils.isNotBlank(entity.getOldmanId()), "oldman_id", entity.getOldmanId());
        return wrapper;
    }

}
